package com.ti.shangou.service.impl;

import com.ti.shangou.dao.RoleDao;
import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.entity.Role;
import com.ti.shangou.pojo.query.PageQuery;
import com.ti.shangou.pojo.vo.PermissionVO;
import com.ti.shangou.pojo.vo.RoleVO;
import com.ti.shangou.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作者：田宝山
 * 日期：2020/5/29
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    RoleDao roleDao;
    @Resource
    PermissionServiceImpl permissionService;

    @Override
    public int updateRoleById(RoleVO roleVO) {
        return roleDao.updateByPrimaryKeySelective(roleVO);
    }

    @Override
    public Integer selectRoleCount() {
        return roleDao.selectRoleCount();
    }

    @Override
    public List<RoleVO> selectRoleList(PageQuery pageQuery) {
        return roleDao.selectRoleList(pageQuery.getStart(),pageQuery.getLimit());
    }

    public ResponseDTO add(Role role){
        return ResponseDTO.get(roleDao.insertSelective(role)==1);
    }

    public List<RoleVO> selectRoleListByUserRoles(String roles){
        List<RoleVO> roleVOS = roleDao.selectHisRolesByRoles(roles);
        return roleVOS;
    }

    public List<RoleVO> getSystemRoles(){
        List<RoleVO> roleVOS = roleDao.selectSystemRoles();
        List<PermissionVO> list = permissionService.selectPermissionsByRoles(roleVOS);
        setPermissionsOfRoles(roleVOS,list);
        return roleVOS;
    }

    public boolean deleteRoleByList( List<RoleVO> roles) {
        return roleDao.deleteRoleByList(roles)==roles.size();
    }
}
